package net.lab1024.sa.admin.module.workflow.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.lab1024.sa.admin.core.domain.ProcessQuery;
import net.lab1024.sa.admin.module.workflow.domain.form.WfDeployVo;
import net.lab1024.sa.admin.module.workflow.domain.form.WfFormVo;
import net.lab1024.sa.admin.module.workflow.service.WfDeployFormService;
import net.lab1024.sa.admin.module.workflow.service.WfDeployService;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.code.WorkFlowErrorCode;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import net.lab1024.sa.base.common.util.JsonUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import java.util.Arrays;
import java.util.Map;
import java.util.Objects;

/**
 * 流程部署
 *
 * @author KonBAI
 * @createTime 2022/3/24 20:57
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/workflow/deploy")
public class WfDeployController {

    private final WfDeployService deployService;
    private final WfDeployFormService deployFormService;

    /**
     * 查询流程部署列表
     */
    @SaCheckPermission("workflow:deploy:list")
    @GetMapping("/list")

    public ResponseDTO<PageResult<WfDeployVo>> list(ProcessQuery processQuery) {
        PageResult<WfDeployVo> pageResult = deployService.queryPageList(processQuery);
        return ResponseDTO.ok(pageResult);
    }

    /**
     * 查询流程部署版本列表
     */
    @SaCheckPermission("workflow:deploy:list")
    @GetMapping("/publishList")
    public ResponseDTO<PageResult<WfDeployVo>> publishList( ProcessQuery processQuery) {
        return ResponseDTO.ok(deployService.queryPublishList(processQuery));
    }

    /**
     * 激活或挂起流程
     *
     * @param state        状态（active:激活 suspended:挂起）
     * @param definitionId 流程定义ID
     */
    @PutMapping(value = "/changeState")
    public ResponseDTO<Boolean> changeState(@RequestParam String state, @RequestParam String definitionId) {
        deployService.updateState(definitionId, state);
        return ResponseDTO.ok(true);
    }

    /**
     * 读取xml文件
     *
     * @param definitionId 流程定义ID
     * @return
     */
    @SaCheckPermission("workflow:deploy:query")
    @GetMapping("/bpmnXml/{definitionId}")
    public ResponseDTO<String> getBpmnXml(@PathVariable(value = "definitionId") String definitionId) {

        String bpmnXml =   deployService.queryBpmnXmlById(definitionId);

        return ResponseDTO.ok(bpmnXml);
    }

    /**
     * 删除流程模型
     *
     * @param deployIds 流程部署ids
     */
    @SaCheckPermission("workflow:deploy:remove")

    @DeleteMapping("/{deployIds}")
    public ResponseDTO<Boolean> remove(@NotEmpty(message = "主键不能为空") @PathVariable String[] deployIds) {
        deployService.deleteByIds(Arrays.asList(deployIds));
        return ResponseDTO.ok(true);
    }

    /**
     * 查询流程部署关联表单信息
     *
     * @param deployId 流程部署id
     */
    @GetMapping("/form/{deployId}")
    public ResponseDTO<?> start(@PathVariable(value = "deployId") String deployId) {
        WfFormVo formVo = deployFormService.selectDeployFormByDeployId(deployId);
        if (Objects.isNull(formVo)) {
            return ResponseDTO.error(WorkFlowErrorCode.WF_FORM_NOT_EXIST);
        }
        return ResponseDTO.ok(formVo);
    }
}
